//
//  tutu_parse_body.c
//  解析服务器返回的内容，并处理对应逻辑
//
//  Created by ffly on 2021/3/15.
//

#include "xlx_parse_body.h"
#include "xlx_tcpsocket.h"
#include "xlx_os.h"
#include "xlx_func.h"
#include "xlx_core.h"
#include "xlx_define.h"

typedef void (*PARSE_FUNC) (TutuMsgHead *head,const char *data,int len);

static PARSE_FUNC parse_body_list[]={
    0,
    tutubody_login,
    tutubody_heart,
    tutubody_synctime,
    tutubody_single_gps,
    tutubody_multiple_gps,
    tutubody_lbs,
    tutubody_audio,
    tutubody_alert,
    tutubody_stringaction,
    0,
    0
};

//动态选择对应的函数进行包体解析
void tutubody_callfunc(TutuMsgHead *head,const char *data,int len){
    if(head->cmd>= 0x01 && head->cmd <= 0x0a && parse_body_list[head->cmd]){
        TUTULOG("recv cmd=%d bodylen=%d",head->cmd,len);
        parse_body_list[head->cmd](head,data,len);
    }
}

//同步系统时间
//utc0:单位秒
static void tutu_synctime(u32 utc0){
    if(g_tutuos.set_rtc_time && utc0 > 0){
        TutuTime t = {0};
        xlx_localtime(utc0,&t);
        t.mon = t.mon + 1;
        t.hour = t.hour + 8;//按照本地+8时区
        g_tutuos.set_rtc_time(&t);
    }
}

//登录
void tutubody_login(TutuMsgHead *head,const char *data,int len){
    TutuMsgLoginRsp rsp = {0};
    memcpy(&rsp, data, sizeof(TutuMsgLoginRsp));
    if(tutu_is_bigendian == 0){
        rsp.utctime = xlx_htonl(rsp.utctime);
        rsp.port = xlx_htons(rsp.port);
        rsp.ip = xlx_htonl(rsp.ip);
    }
    
    if(rsp.result == 0){
        if(rsp.ip > 0 && rsp.port > 0){
            //被分配到其他服务器，则准备连接到其他ip地址
            int a = (rsp.ip >> 24) & 0xff;
            int b = (rsp.ip >> 16) & 0xff;
            int c = (rsp.ip >> 8) & 0xff;
            int d = rsp.ip & 0xff;
            TUTUSprintf(g_tutuservercfg.server_host,"%d.%d.%d.%d",a,b,c,d);
            g_tutuservercfg.server_port = rsp.port;

            //还原登陆状态和连接状态，使客户端自动连接到新ip服务
            g_tutuparam.is_login_server = 0;
            g_tutuparam.server_connected = 0;
            TUTULOG("forward new server = %s:%d",g_tutuservercfg.server_host,rsp.port);
        }else{
            g_tututcp.allow_writing(1);
            g_tutuparam.is_login_server = 1;
            g_tututcp.load_offline_data();
        }
        tutu_synctime(rsp.utctime);
    }

    TUTULOG("\nrecv login:status:%d serverTime:%ld addr:%ld port:%d\n",rsp.result,rsp.utctime,rsp.ip,rsp.port);
}

//心跳
void tutubody_heart(TutuMsgHead *head,const char *data,int len){
    TUTULOG("\nrecv heart:\n");
}

//同步时间
void tutubody_synctime(TutuMsgHead *head,const char *data,int len){

    
    TutuMsgSyncTimeRsp rsp = {0};
    memcpy(&rsp, data, sizeof(TutuMsgSyncTimeRsp));
    
    if(tutu_is_bigendian == 0){
        rsp.utctime = xlx_htonl(rsp.utctime);
        tutu_synctime(rsp.utctime);
    }
    
    TUTULOG("recv synctime:%ld \n",rsp.utctime);
}

//上报单GPS
void tutubody_single_gps(TutuMsgHead *head,const char *data,int len){
    TUTULOG("\nrecv single gps:\n");
}

//上报多GPS
void tutubody_multiple_gps(TutuMsgHead *head,const char *data,int len){
    // multi_gps_upload_point_count_sub();
    TUTULOG("recv mul gps:05");
}

//复合定位
void tutubody_lbs(TutuMsgHead *head,const char *data,int len){
    TUTULOG("\nrecv lbs:\n");
}

//声控报警
void tutubody_audio(TutuMsgHead *head,const char *data,int len){
    TUTULOG("\nrecv record audio:\n");
}

//告警
void tutubody_alert(TutuMsgHead *head,const char *data,int len){
    TUTULOG("\nrecv alert:\n");
}

//字符指令集
void tutubody_stringaction(TutuMsgHead *head,const char *data,int len){
    u16 aid = 0;
    memcpy(&aid,data,2);
    TUTULOG("\nrecv string command: seq=%x\n",head->seq);
    
    aid = xlx_htons(aid);
    xlx_action_process_data(ACTION_TYPE_WEB,aid,0,data+2,len-2);
}
